<?php

namespace app\zyhsapi\model\good_rtc_live;

use app\zyhsapi\model\BaseCacheZyhs;
use XznsTools\cache\zyhs\CacheManageZyhs;

class ZyhsGoodRtcLiveNoMsgRecord extends BaseCacheZyhs
{
    protected $table = 'good_wechat.zyhs_good_rtc_live_no_msg_record';

    protected $cacheModel = 'zyhs.good_rtc_live_disenable_speak';

    public function SaveData(array $data)
    {
        try
        {
            $id = $this->insertGetId($data);

            CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $data['app_type'] . ':' . $data['uid'])->rmCache();

            return (int)$id;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
    }

    /**
     * 获取 用户禁言状态
     * @param int $appType 1=>准有好事 2=>准了APP
     * @param int $uid
     * @return bool
     * @throws \Exception
     */
    public function getDisenableSpeakStatus(int $appType, int $uid)
    {
        $data = CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $appType . ':' . $uid)->getDataAndSet(function () use ($appType, $uid)
        {
            try
            {
                return $this->where(['uid' => $uid, 'status' => 1, 'app_type' => $appType])->order('id desc')->findOrFail()->toArray();
            }
            catch (\Exception $e)
            {
                return [''];
            }
        });

        return (bool)array_filter($data);
    }
}
